utils.js ➔ getApiCalls   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
c 0
b 0
f 0
rs 9.9
cc 3
1
import path from 'path';
2
import { pause } from 'myrmidon';
3
import mockStdin from  'mock-stdin';
4
import { stdout } from 'test-console';
5
import { assert } from 'chai';
6
import { getNamespace } from 'cls-hooked';
7
import jsonQuery from 'json-query';
8
import { entry } from './constants';
9
import { apiTraces } from './logger';
10
11
12
export class CLITester {
13
    constructor(dialog, factory) {
14
        this.dialog = dialog;
15
        this.stdin = mockStdin.stdin();
16
        this.factory = factory;
17
    }
18
19
    async test(index = 0, time = 100) {
20
        const inspect = stdout.inspect();
21
22
        await pause(time);
23
        inspect.restore();
24
        const out = inspect.output.join('\n');
25
26
        if (index === this.dialog.length) {
27
            this.stdin.restore();
28
29
            return out;
30
        }
31
32
        const item = this.dialog[index];
33
34
        this.stdin.send([ item.input, null ]);
35
        this.factory.logger.log('info', { item, out, service: 'CLITester' });
36
        assert.match(out, new RegExp(item.output), JSON.stringify(item));
37
38
        return [ out, await this.test(index + 1) ].join('\n');
39
    }
40
}
41
42
export async function getApiCalls(query, { trace = true, traces = apiTraces } = {}) {
43
    const ns = getNamespace('__TEST__');
44
    const queryItems = [];
45
46
    if (query)queryItems.push(query);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
47
48
    if (trace) {
49
        const traceId = ns.get('current').id;
50
51
        queryItems.push(`traceId=${traceId}`);
52
    }
53
54
    const q = `[*${queryItems.join('&')}]`;
55
    const res = jsonQuery(q, { data: traces });
56
57
    return res.value;
58
}
59
60
export function load(relPath, clearCache) {
61
    const absPath = path.resolve(entry, relPath);
62
63
    if (clearCache) delete require.cache[require.resolve(absPath)];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
64
    // eslint-disable-next-line security/detect-non-literal-require
65
    const result =  require(absPath);
66
67
    if (clearCache) delete require.cache[require.resolve(absPath)];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
68
69
    return result;
70
}
71
72
export function resolve(relPath) {
73
    return require.resolve(path.join(entry, relPath));
74
}
75